const mongoose = require('mongoose');
// const path = require('path');
const winston = require('../utils/winstonLogger.js');
const foodModel = mongoose.model('Food');

module.exports = {
    addFood : addFood,
    updateFood : updateFood,
    deleteFood : deleteFood,
    queryFood : queryFood,
    queryFoodByType: queryFoodByType
}

function addFood (foods,callback) {
    foodModel.insertMany(foods,function(error,docs){
        if(error){
            winston.error(error);
            return  callback(error,null);
        }
        else{
            console.log(docs);
            return  callback(null,docs);
        }
    })
}

function deleteFood (foodId,callback) {
    foodModel.deleteMany({_id:foodId},function(error,docs){
        if(error){
            winston.error(error);
            return  callback(error,null);
        }
        else{
            console.log(docs);
            return  callback(null,docs);
        }
    })
}

function updateFood (newfood,callback) {
    foodModel.updateMany({_id:newfood._id},newfood,function(error,docs){
        if(error){
            winston.error(error);
            return  callback(error,null);
        }
        else{
            console.log(docs);
            return  callback(null,docs);
        }
    })
}

function queryFood ($n={},callback){
    foodModel.find($n,function(error,docs){
        if(error){
            winston.error(error);
            return  callback(error,null);
        }
        else{
            console.log(docs);
            return  callback(null,docs);
        }
    });
}

function queryFoodByType ($n={},callback){
    foodModel.find($n,function(error,docs){
        if(error){
            winston.error(error);
            return  callback(error,null);
        }
        else{
            console.log(docs);
            return  callback(null,docs);
        }
    }).populate('merchant').exec();
}


